//
//  SmallDecisionsTextView.h
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/26.
//

#import "SmallDecisionsTextView.h"

@interface SmallDecisionsTextView () <UITextViewDelegate>

@property (nonatomic, assign) CGFloat originalHeight;
@property (nonatomic, strong) UILabel *placeHolderLabel;

@end


@implementation SmallDecisionsTextView

- (instancetype)init {

    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.showNumbers = 0;
        [self placeHolderLabel];
        self.delegate = self;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.showNumbers = 0;
        [self placeHolderLabel];
        self.textContainerInset = UIEdgeInsetsZero;
        self.delegate = self;
    }
    return self;
}

- (UILabel *)placeHolderLabel {

    if (!_placeHolderLabel) {

        _placeHolderLabel = [[UILabel alloc] init];
        _placeHolderLabel.numberOfLines = 0;
        _placeHolderLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_placeHolderLabel];
    }
    return _placeHolderLabel;
}

- (void)textViewDidChange:(UITextView *)textView {

    if (self.placeholdTextViewDelegate && [self.placeholdTextViewDelegate respondsToSelector:@selector(textViewChange:)]) {

        [self.placeholdTextViewDelegate textViewChange:self];
    }
    if (textView.text.length > 0) {

        self.placeHolderLabel.hidden = YES;
    } else {

        self.placeHolderLabel.hidden = NO;
    }

    if (self.showNumbers == 0) {

        return;
    }

    if (self.originalHeight == 0 ) {

        self.originalHeight = textView.height;
    }

    CGSize size = [self.text sizeForFont:self.font size:CGSizeMake(self.width - self.contentInset.left - self.contentInset.right - self.textContainerInset.left - self.textContainerInset.right - 10, 1000) mode:NSLineBreakByCharWrapping | NSLineBreakByWordWrapping];

    NSInteger i = size.height / self.font.lineHeight;
    if (i > self.showNumbers) {

        i = self.showNumbers;
    }

    CGFloat height = self.font.lineHeight * i + self.contentInset.top + self.contentInset.bottom + self.textContainerInset.top + self.textContainerInset.bottom;
    if (self.originalHeight > height) {

        height = self.originalHeight;
    }

    self.height = height;

    if (self.height > self.originalHeight) {

        [self scrollToBottomAnimated:YES];
    }

    if (self.placeholdTextViewDelegate && [self.placeholdTextViewDelegate respondsToSelector:@selector(textView:changeHeight:)]) {

        [self.placeholdTextViewDelegate textView:self changeHeight: height];
    }
}

- (void)setText:(NSString *)text {

    [super setText:text];
    [self textViewDidChange:self];
}

- (void)setPlaceholder:(NSString *)placeholder {

    _placeholder = placeholder;
    self.placeHolderLabel.text = placeholder;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {

    _placeholderColor = placeholderColor;
    self.placeHolderLabel.textColor = placeholderColor;
}

- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset {

    [super setTextContainerInset:textContainerInset];

    CGFloat left = textContainerInset.left + 5;
    CGFloat top  = textContainerInset.top;
    [self.placeHolderLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(left);
        make.top.mas_equalTo(top);
    }];
}

- (void)setFont:(UIFont *)font {

    [super setFont:font];
    self.placeHolderLabel.font = font;
}

- (BOOL)canBecomeFirstResponder {
    return self.editable;
}

@end